<html>
<div id="myheader">
<?php
session_start();

error_reporting(E_ALL);

$user_id = $_SESSION['user_id'];
$user_name = $_SESSION['user_name'];

if (!isset($user_id ) )
{
echo "<h1> Please login</h1>";
echo "<br />";
header("Location: /OSPMS/index.php");
}
?>
</div>


<title>OSPMS</title>
<head>


</head>


<link rel="stylesheet" href="style/ospms-body.css" type="text/css">

<body>
<div id="mymajorbody">
<div id="mybody">

<?php

echo "<table style='border: solid 1px black;'>";

$servername = "localhost";
$username = "ospms";
$password = "ospms";
$dbname = "OSPMS";


class TableRows extends RecursiveIteratorIterator {
    function __construct($it) {
        parent::__construct($it, self::LEAVES_ONLY);
    }

    function current() {
        return "<td style='width:150px;border:1px solid black;'>" . parent::current(). "</td>";
    }

    function beginChildren() {
        echo "<tr>";
    }

    function endChildren() {
        echo "</tr>" . "\n";
    }
}


try {
    $conn = new PDO("mysql:host=$servername;dbname=$dbname", $username, $password);
    $conn->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);


$select = $conn->query('SELECT * FROM users_privilege');

$total_column = $select->columnCount();
//var_dump($total_column);

for ($counter = 0; $counter <= $total_column; $counter ++) {
    $meta = $select->getColumnMeta($counter);
    $column[] = $meta['name'];
}
//print_r($column);


$stmt = $conn->prepare("DESCRIBE users_privilege");
$stmt->execute();
$table_fields = $stmt->fetchAll(PDO::FETCH_COLUMN);
//print_r($table_fields);

	echo "<tr>". " \n";
for ( $i=0 ; $i<count($table_fields); $i++ ) {
	echo "<td>" ;
	print_r($table_fields[$i]);
	echo "</td>" ;
}
	echo "</tr>". " \n";

    $stmt = $conn->prepare("SELECT * FROM users_privilege");
    $stmt->execute();
    // set the resulting array to associative
    $result = $stmt->setFetchMode(PDO::FETCH_ASSOC);

    foreach(new TableRows(new RecursiveArrayIterator($stmt->fetchAll())) as $k=>$v) {
        echo $v;
    }
    $dsn = null;
    }
catch(PDOException $e)
    {
    echo "Error: " . $e->getMessage();
    }
$conn = null;
echo "</table>";
?> 

</div>
</div>


